//
//  MediaPlayer_Table_Cell.m
//  Food
//
//  Created by dllo on 15/7/20.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "MediaPlayer_Table_Cell.h"
#import "BaseHeader.h"
#import "UIImageView+WebCache.h"
#import "UIView+WebCacheOperation.h"
@implementation MediaPlayer_Table_Cell

- (void)dealloc
{
    [_movieplayer release];
    [super dealloc];
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        _movieplayer = [[MPMoviePlayerController alloc] init];
        [ _movieplayer prepareToPlay];
        _movieplayer.controlStyle = MPMovieControlStyleDefault;
        _movieplayer.shouldAutoplay = NO;
        [self.contentView addSubview:_movieplayer.view];
        
        self.backGroundimage = [[UIImageView alloc] init];
        [self.backGroundimage setImage:[UIImage imageNamed:@"240425-12101Z10J013-2.jpg"]];
        
        [self.backGroundimage setUserInteractionEnabled:YES];
        
        [self.contentView addSubview:self.backGroundimage];
        
        self.button = [UIButton buttonWithType:UIButtonTypeSystem];
        [self.button setBackgroundImage:[UIImage imageNamed:@"player.png"] forState:UIControlStateNormal];
        [self.backGroundimage addSubview:self.button];
        
        [self.button addTarget:self action:@selector(player) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

- (void) nextMovie:(NSNotification *) note
{
        if (note.object == self.movieplayer) {
        NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
        if (reason == MPMovieFinishReasonPlaybackEnded) {
            _movieplayer.contentURL = [NSURL URLWithString:self.productionProcessPath];
            [self player];
        }
    }
}

-(void)setPlayURL:(NSString *)URL image:(NSString *) image
{
    _movieplayer.contentURL = [NSURL URLWithString:URL];
    [self.backGroundimage sd_setImageWithURL:[NSURL URLWithString:image]];
}
- (void) player
{
        self.backGroundimage.hidden = YES;
        [_movieplayer play];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(nextMovie:)
                                                     name:MPMoviePlayerPlaybackDidFinishNotification
                                                   object:self.movieplayer];
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    [_movieplayer.view setFrame:CGRectMake(0,0,ScreenWidth,200 / 375.0 *ScreenWidth)];
    _movieplayer.view.center = self.contentView.center;
    self.backGroundimage.frame = CGRectMake(0,0,ScreenWidth,200 / 375.0 *ScreenWidth);
    self.button.frame = CGRectMake(0, 0, 50, 50);
    self.button.center = self.backGroundimage.center;
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
